﻿/*
创建人：zhangrj
日期：2023-12-28
描述：批次型别维护js
*/
var page = {
    pageTotal: 0,
    pageSize: 20,
    pageNumber: 1,
    rootPath: getRootPath(), //网站根路径
    url: getRootPath() + "/Ashx/AutoStartContainerHandler.ashx",
    commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        editPage.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        $("#dgOperaLog").operaLogDialog(); //初始化操作日志
    },
    //页面事件绑定
    bind: function () {
        //列表加载
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pageTotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
                 { field: 'PRODUCTNAME', title: '件号', width: 150 },
                 { field: 'CONTAINERNAME', title: '批次号', width: 200 },
                 { field: 'FAMILYLISTNAMES', title: '型别', width: 180 },
                 { field: 'QTY', title: '在制数', width: 100 },
                 { field: 'notes', title: '修改记录', width: 60,
                     formatter: function (value, row, index) {
                         return '<button class="btnNews" type="button" onclick="page.loadLogData(\'' + row.CONTAINERID + '\')">查看</button>'
                     }
                 }
                ]]
        });
        //查询按钮
        $('#btnSearch').on("click", function () {
            page.pageNumber = 1;
            page.searchData();
        });
        //修改按钮
        $('#btnEdit').on("click", function () {
            page.clearPage(false, false);
            page.openDialog();
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.searchData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.searchData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.clearPage(true, true);
            page.pageNumber = 1; page.pageTotal = 0
            $('#pp').pagination('refresh', {
                total: page.pageTotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //查询
    searchData: function () {
        var params = {
            pageSize: page.pageSize,
            pageIndex: page.pageNumber
        };
        var queryFormData = $("#queryForm").serializeArray();
        $.each(queryFormData, function () {
            params[this.name] = this.value;
        });
        page.post(page.url, { action: "GetContainerProductFamilyList", params: JSON.stringify(params) }, true, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            page.pageTotal = res.RowCount;
            $("#gdContainerInfo").datagrid('loadData', { total: res.RowCount, rows: res.DBTable });
        });
    },
    //打开编辑弹窗
    openDialog: function () {
        editPage.selectRows = [];
        var selectRow = $("#gdContainerInfo").datagrid("getChecked");
        if (selectRow.length === 0) {
            displayMessage("请选择数据", false);
            return false;
        }
        page.disableBtn(); //禁用主页面按钮
        editPage.selectRows = selectRow; //记录编辑的数据ID
        editPage.init();
    },
    //打开操作日志
    loadLogData: function (containerID) {
        $("#dgOperaLog").operaLogDialog({ parentID: containerID, operaType: "ContainerFamilyModify" });
    },
    //禁用页面按钮
    disableBtn: function () {
        $("#mainSec").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('disable');
        });
    },
    //启用页面按钮
    enableBtn: function () {
        $("#mainSec").find(".easyui-linkbutton").each(function () {
            $(this).linkbutton('enable');
        });
    },
    //清除页面（查询区域，列表区域，填写区域，是否清空消息）
    clearPage: function (query, list) {
        if (query) {
            $("#txtProductName").textbox("clear");
            $("#txtContainerName").textbox("clear", "");
            $("#txtOrderName").combobox("clear");
            $("#txtProductFamilyName").textbox("clear");
        }
        if (list) {
            clearEasyuiDataGrid("gdContainerInfo"); //清空列表
        }
    },
    //post数据（路径，参数，是否异步，成功返回方法）
    post: function (url, param, async, callback) {
        $.ajax({
            type: "Post",
            url: url,
            data: param,
            cashe: false,
            async: async,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
                $("body").find(".easyui-linkbutton").each(function () {
                    $(this).linkbutton('enable');
                });
            }
        });
    }
};
//编辑对象
var editPage = {
    selectRows: [], //编辑选择的数据
    //初始化
    init: function () {
        editPage.clearPage();
        editPage.initPage(); //初始化页面数据
    },
    //绑定事件
    bind: function () {
        //规则弹窗保存
        $('#btnRuleSave').on("click", function () {
            editPage.saveData();
        });
        //规则弹窗取消
        $('#btnRuleCancel').on("click", function () {
            $('#dg').dialog("close");
        });
        $('#dgProductFamilyType').combobox({
            onSelect: function (record) {
                editPage.bindProductFamily(record.value);
            }
        });
        $("#dgSltProductFamily").combogrid({
            multiple: true, //设置允许多选
            panelWidth: 150,
            panelHeight: 200,
            idField: 'PRODUCTFAMILYID',
            textField: 'PRODUCTFAMILYNAME',
            checkOnSelect: true,
            columns: [[
                                { field: 'ck', title: '', width: 50, checkbox: true },
                                { field: 'PRODUCTFAMILYID', title: '', width: 60, hidden: true },
                                { field: 'PRODUCTFAMILYNAME', title: '机型', width: 110 }
                            ]]
        });
    },
    //初始化页面数据
    initPage: function () {
        if (editPage.selectRows.length === 1) {
            page.post(page.url, { action: "InitProductFamilyDialogData", containerID: editPage.selectRows[0].CONTAINERID }, false, function (res) {
                if (!res.Result) {
                    page.enableBtn();
                    displayMessage(res.Message, res.Result);
                    return;
                }
                if (res.ReturnData !== null) {
                    $("#dgProductFamilyType").combobox("setValue", res.ReturnData);
                    var familyDatas = [];
                    for (var i = 0; i < res.DBTable.length; i++) {
                        familyDatas.push(res.DBTable[i].PRODUCTFAMILYLISTID);
                    }
                    $("#dgSltProductFamily").combogrid("setValues", familyDatas);
                }
            });
        }
        $('#dg').dialog({
            onClose: function () {
                page.enableBtn();
            }
        });
    },
    //绑定机型
    bindProductFamily: function (familyType) {
        if (familyType === "" || familyType === null) {
            $("#dgSltProductFamily").combogrid('clear');
            return;
        }
        page.post(page.url, { action: "GetProductFamilyData", familyType: familyType }, false, function (res) {
            if (!res.Result) {
                displayMessage(res.Message, res.Result);
                return;
            }
            $("#dgSltProductFamily").combogrid({
                data: res.DBTable //下拉数据
            });
        });
    },
    //保存数据
    saveData: function () {
        if (editPage.selectRows.length === 0) {
            displayMessage("选择数据过期，请关闭弹窗重新编辑", false);
            return;
        }
        var vaild = $('#dgFamilyForm').form('validate');
        if (vaild === false) {
            displayMessage("请填写必填项", false);
            return;
        }
        var pfSelectRow = $('#dgSltProductFamily').combogrid('grid').datagrid('getChecked');
        if (pfSelectRow.length === 0) {
            displayMessage("请选择机型", false);
            return;
        }
        var params = {
            action: "SaveContainerProductFamily",
            modifyReason: $('#dgtxtModifyReason').combobox('getText'), //修改理由
            familyTypeName: $('#dgProductFamilyType').combobox('getText'),//机型类型
            containerDatas: JSON.stringify(editPage.selectRows), //选择的批次
            productFamilyDatas: JSON.stringify(pfSelectRow)//选择的机型
        };
        page.post(page.url, params, true, function (res) {
            if (res.Result) {
                $('#dg').dialog("close");
                page.searchData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //清空页面数据
    clearPage: function () {
        $("#dgProductFamilyType").combobox("clear");
        $("#dgSltProductFamily").combogrid("clear");
        $("#dgtxtModifyReason").textbox("clear");
    }
};
$(function () {
    page.init();
});